package com.xm2.demo.utils;

import org.springframework.data.domain.Page;

import java.util.Collections;
import java.util.List;

/**
 * @Date 2018/8/3 09:38
 * @Description 分页工具类
 */
public class Pagination {
    // 默认每页显示的记录条数
    private final Integer PAGE_DATA_SIZE = 5;
    // 记录总条数
    private Integer totalRows;
    // 记录总页数
    private Integer totalPage;
    // 当前的页数
    private Integer currentPage;
    // 每一页显示的记录的条数
    private Integer size;
    // 存放返回值的集合
    private List<?> list;

    private Page<?> page;
    //开始的行数
    private Integer startRow;

    public Integer getPAGE_DATA_SIZE() {
        return PAGE_DATA_SIZE;
    }

    public Page<?> getPage() {
        return page;
    }

    public void setPage(Page<?> page) {
        this.page = page;
    }

    public Integer getTotalRows() {
        return totalRows;
    }

    public void setTotalRows(Integer totalRows) {
        this.totalRows = totalRows;
        int totalPage = totalRows % getSize() == 0 ? totalRows / getSize() : totalRows / getSize() + 1;
       setTotalPage(totalPage);
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public Integer getCurrentPage() {
        currentPage = currentPage == null ? 0 : currentPage;
        currentPage = currentPage >= totalPage ? totalPage : currentPage;
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public final Integer getSize() {
        return size == null ? PAGE_DATA_SIZE : size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }

    public List<?> getList() {
        return list == null ? Collections.EMPTY_LIST : list;
    }

    public void setList(List<?> list) {
        this.list = list;
    }


    public Integer getStartRow() {
        if (startRow == null) {
            startRow = getCurrentPage() == 0 ? 0 : (currentPage - 1) * size;
        }
        return startRow;
    }

    public void setStartRow(Integer startRow) {
        this.startRow = startRow;
    }
}
